package com.copa.module.goods.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.copa.domain.PageVo;
import com.copa.domain.ResponseDto;
import com.copa.module.goods.domain.dto.GoodsDetailDto;
import com.copa.module.goods.domain.dto.GoodsDto;
import com.copa.module.goods.domain.entity.*;
import com.copa.module.goods.domain.vo.GoodsAddVo;
import com.copa.module.goods.domain.vo.GoodsProductVo;
import com.copa.module.goods.domain.vo.GoodsUpdateVo;
import com.copa.module.goods.manage.GoodsManage;
import com.copa.module.goods.mapper.GoodsDao;
import com.copa.module.goods.service.GoodsService;
import com.copa.module.order.domain.dto.OrderListDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author copa
 * @createDate 2021-01-08 12:28
 * @function
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsManage goodsManage;

    @Autowired
    private GoodsDao goodsDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDto<String> addGoods(GoodsAddVo goodsAddVo) {
        // 校验参数
        if (ObjectUtil.length(goodsAddVo.getGoodsAttributeVo()) == 0
                || ObjectUtil.length(goodsAddVo.getGoodsProductVo()) == 0) {
            return ResponseDto.errorMsg("请添加产品或规格！");
        }

        // 查找类目是否存在
        ResponseDto<CategoryEntity> categoryRes = goodsManage.findCategoryByCategoryId(goodsAddVo.getCategoryId());
        if (categoryRes.isNotSuccess()) {
            return ResponseDto.errorMsg(categoryRes.getMsg());
        }

        // 添加商品
        GoodsEntity goods = new GoodsEntity();
        BeanUtils.copyProperties(goodsAddVo, goods);
        int insert = goodsDao.insert(goods);
        if (insert == 0) {
            return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
        }
        // 商品id
        Long goodsId = goods.getId();

        ResponseDto<Boolean> flag;

        // 商品参数处理
        flag = goodsManage.addGoodsAttribute(goodsAddVo.getGoodsAttributeVo(), goodsId);
        if (flag.isNotSuccess()) {
            return ResponseDto.errorMsg(flag.getMsg());
        }

        // 商品产品处理
        // 预处理：若sku没有加图片，则这里自动补充为商品主图
        for (GoodsProductVo goodsProductVo : goodsAddVo.getGoodsProductVo()) {
            if (goodsProductVo.getPicUrl() == null || "".equals(goodsProductVo.getPicUrl())) {
                goodsProductVo.setPicUrl(goodsAddVo.getPicUrl());
            }
        }
        flag = goodsManage.addGoodsProduct(goodsAddVo.getGoodsProductVo(), goodsId);
        if (flag.isNotSuccess()) {
            return ResponseDto.errorMsg(flag.getMsg());
        }

        // 商品规格处理
        flag = goodsManage.addGoodsSpecifications(goodsAddVo.getGoodSpecificationsVo(), goodsId);
        if (flag.isNotSuccess()) {
            return ResponseDto.errorMsg(flag.getMsg());
        }

        return ResponseDto.successMsg("添加商品成功！");
    }

    @Override
    public ResponseDto<IPage<GoodsDto>> list(PageVo pageVo, String goodsName) {
        Page<GoodsDto> page = new Page<>(pageVo.getCurrent(), pageVo.getSize());
        //Page<GoodsEntity> goodsEntities = goodsDao.selectPage(new Page<>(pageVo.getCurrent(), pageVo.getSize()), null);
        IPage<GoodsDto> goodsDtoIPage = goodsDao.selectGoodsListPage(page, goodsName);
        if (ObjectUtil.isNotNull(goodsDtoIPage)) {
            return ResponseDto.success(goodsDtoIPage);
        }
        return ResponseDto.errorMsg("抱歉，没查到该商品信息！");
    }

    @Override
    public ResponseDto<GoodsDetailDto> detail(Long goodsId) {
        // 查商品
        GoodsEntity goods = goodsDao.selectById(goodsId);
        if (ObjectUtil.isEmpty(goods)) {
            return ResponseDto.errorMsg("该商品不存在！");
        }

        // 查产品
        ResponseDto<List<GoodsProductEntity>> goodsProductEntities =
                goodsManage.findGoodsProductByGoodsId(goodsId);
        if (goodsProductEntities.isNotSuccess()) {
            return ResponseDto.errorMsg(goodsProductEntities.getMsg());
        }

        // 查规格
        ResponseDto<List<GoodsAttributeEntity>> goodsAttributeEntities=
                goodsManage.findGoodsAttributeByGoodsId(goodsId);
        if (goodsProductEntities.isNotSuccess()) {
            return ResponseDto.errorMsg(goodsProductEntities.getMsg());
        }

        // 查单规格
        ResponseDto<List<GoodsSpecificationsEntity>> goodsSpecificationsEntities =
                goodsManage.findGoodsSpecificationsByGoodsId(goodsId);

        GoodsDetailDto goodsDetailDto = GoodsDetailDto.builder()
                .goods(goods)
                .goodsProductEntities(goodsProductEntities.getData())
                .goodsAttributeEntities(goodsAttributeEntities.getData())
                .goodsSpecificationsEntities(goodsSpecificationsEntities.getData())
                .build();

        return ResponseDto.success(goodsDetailDto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDto<String> put(GoodsUpdateVo goodsUpdateVo) {

        // 修改规格
        ResponseDto<String> attributeRes = goodsManage.putGoodsAttribute(goodsUpdateVo);
        if (attributeRes.isNotSuccess()) {
            return ResponseDto.errorMsg(attributeRes.getMsg());
        }

        // 修改产品
        ResponseDto<String> productRes = goodsManage.putGoodsProduct(goodsUpdateVo);
        if (productRes.isNotSuccess()) {
            return ResponseDto.errorMsg(productRes.getMsg());
        }

        // 修改规格
        ResponseDto<String> specificationsRes = goodsManage.putGoodsSpecifications(goodsUpdateVo);
        if (specificationsRes.isNotSuccess()) {
            return ResponseDto.errorMsg(specificationsRes.getMsg());
        }

        // 修改商品
        GoodsEntity goods = new GoodsEntity();
        BeanUtils.copyProperties(goodsUpdateVo, goods);
        int update = goodsDao.updateById(goods);
        if (update == 0) {
            return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
        }

        return ResponseDto.successMsg("修改商品成功！");
    }
}
